import {
  config
} from '../utils/config.js'

const tips = {
  1: '抱歉，出现了一个错误',
}
// # 解构
class HTTP {
  request({
    url,
    data = {},
    method = 'GET'
  }) {
    return new Promise((resolve, reject) => {
      this._request(url, resolve, reject, data, method, false)
    })
  }
  requestfile({
    url,
    data = {},
    method = 'GET'
  }) {
    return new Promise((resolve, reject) => {
      this._request(url, resolve, reject, data, method, true)
    })
  }
  _request(url, resolve, reject, data = {}, method = 'GET', isApi = false) {
    let urlApi = config.api_base_url + url, that = this
    if (isApi){
      urlApi = config.file_base_url + url
    }
    wx.request({
      url: urlApi,
      method: method,
      data: data,
      header: {
        "source": "wx",
        Authorization:"Bearer "+wx.getStorageSync('token')
      },
      success: (res) => {
        const code = res.statusCode.toString()
        if (res && res.statusCode && res.statusCode == 403){
          wx.showToast({
            title: '参数错误',
            icon: 'none',
            duration: 2000
          })
          return
        }
        if (code.startsWith('2')) {
          const errorCode =  res.data.errorCode
          if (errorCode == 1009) {
            wx.removeStorageSync('token') 
            wx.login({
              success: res => {
                let code = res.code
                wx.request({
                  url:  config.api_base_url + 'login/submit',
                  method: 'POST',
                  header: {
                      'content-type': 'application/json',
                      //'sid': wx.getStorageSync('shop_id'),
                  },
                  data: {
                      code: code,
                  },
                  success: function (res) {
                      if (res.data.success) {
                          wx.setStorageSync('token', res.data.data.access_token);
                          wx.setStorageSync('token_type', res.data.data.token_type);
                          that._request(url, resolve, reject, data = {}, method = 'GET', isApi = false)
                      } else{
                          wx.setStorageSync('token','1');
                          wx.showModal({
                            title: '友情提示',
                            content: res.data.errorMsg
                          })
                      }
                  },
                  fail(error) {
                      wx.showModal({
                          title: '请求服务提示',
                          content: '请求的服务无法访问、网络错误（打开调试模式）',
                          success(res) {
                            if (res.confirm) {
                              
                            } else if (res.cancel) {
                            }
                          }
                        })
                        
                        wx.setStorageSync('token','1');
                      reject(error.data)
                  }
              })
              }
            })
            return
          }
          resolve(res.data)
        } else {
          console.log('99999999999999999999999')
        console.log(res)
          reject()
          const error_code = res.data.error_code
          const success = res.data.success
          const msg = res.data.errorMsg
          this._show_error(error_code, success, msg)
        }
      },
      fail: (err) => {
        console.log('failfailfailfailfailfailfailfailfail')
        console.log(err)
        reject()
      }
    })
  }

  _show_error(error_code, success, msg) {
    if (error_code != 1000 || !success) {
      wx.showToast({
        title: msg,
        icon: 'none',
        duration: 2000
      })
    }
  }
}
export {
  HTTP
}